package com.controller;

import com.common.Result;
import com.pojo.dto.DeleteCartDto;
import com.pojo.dto.OrderGoodsDto;
import com.pojo.vo.CartVo;
import com.service.ICartService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : 杨世博
 * @date: Created in 15:27 2023/6/3
 */
@RestController
@RequestMapping("/goods")
@Api(tags = "商品接口")
@Slf4j
public class CartController {

    @Autowired
    ICartService cartService;

    /**
     * 将商品添加进购物车
     * @param uid 用户id
     * @param goodsId 商品id
     * @param cartNum 商品数量
     * @return
     */
    @PutMapping("/addCart")
    public Result<Boolean> addCart(@RequestParam("uid")Integer uid,
                                   @RequestParam("goodsId")Integer goodsId,
                                   @RequestParam("cartNum")Integer cartNum){
        cartService.addCart((long) uid, goodsId, cartNum);
        return Result.success();
    }

    /**
     * 获取购物车列表
     * @param uid
     * @return
     */
    @GetMapping("/getCartList")
    public Result<List<CartVo>> getCartList(@RequestParam("uid")Integer uid){
        List<CartVo> carts = cartService.getCartList((long) uid);
        return Result.success(carts);
    }

    /**
     * 修改购物车内商品数量
     * @param uid
     * @param goodsId
     * @param cartNum
     * @return
     */
    @PostMapping("/updateGoodsNum")
    public Result<List<CartVo>> updateGoodsNum(@RequestParam("uid")Integer uid,
                                             @RequestParam("goodsId")Integer goodsId,
                                             @RequestParam("cartNum")Integer cartNum){
        List<CartVo> carts = cartService.updateGoodsNum((long) uid, goodsId, cartNum);
        return Result.success(carts);
    }

    /**
     * 删除购物车内的商品
     * @param uid
     * @param goodsId
     * @return
     */
    @DeleteMapping("/deleteGoodsNum")
    public Result<List<CartVo>> deleteGoodsNum(@RequestParam("uid")Integer uid,
                                             @RequestParam("goodsId")Integer goodsId){
        List<CartVo> carts = cartService.deleteGoodsNum((long) uid, goodsId);
        return Result.success(carts);
    }

    /**
     * 删除购物车内的商品
     */
    @PostMapping("/deleteCartGoods")
    public Result<List<CartVo>> deleteGoods(@RequestBody OrderGoodsDto orderGoodsDto){

        System.out.println("\n\n\n\n\n\n\n\n"+orderGoodsDto+"\n\n\n\n\n\n\n");

        List<CartVo> carts = cartService.deleteGoods(orderGoodsDto);
        return Result.success(carts);
    }
}
